//
//  TaoLun.m
//  ZhiBoYuan
//
//  Created by apple on 2018/5/9.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "TaoLun.h"

@interface TaoLun ()
<
UITextViewDelegate
>

@property  (nonatomic,strong)UITextView *textview;


@property (nonatomic, strong)UIButton *commiteBtn;

@property (nonatomic, strong)UILabel *textViewPlaceHo;

@property (nonatomic, strong)UIButton *closeBtn;

@end

@implementation TaoLun

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [UIColor whiteColor];
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, NAVIGATION_BAR_HEIGHT)];
        view.backgroundColor = ZhuTiColor;
        [self addSubview:view];
        [view addSubview:self.closeBtn];
        [self addSubview:self.commiteBtn];
        [self addSubview:self.textview];
        [self.textview addSubview:self.textViewPlaceHo];

    }
    return self;
}
-(UILabel *)textViewPlaceHo{
    if (!_textViewPlaceHo) {
        _textViewPlaceHo = [[UILabel alloc]initWithFrame:CGRectMake(5 * KscreenWidth,7 * KscreenHeight, screenWidth - 10 * KscreenWidth, 20 * KscreenHeight)];
        _textViewPlaceHo.text = @"内容不能为空，字数上限200字";
        _textViewPlaceHo.textColor = hui2Color;
        _textViewPlaceHo.font = [UIFont systemFontOfSize:18 * KscreenWidth];
    }
    return _textViewPlaceHo;
}


-(UIButton *)closeBtn
{
    if (!_closeBtn) {
        UIButton *Btn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        Btn.frame = CGRectMake(10, STATUS_BAR_HEIGHT, NAVIGATION_BAR_HEIGHT - STATUS_BAR_HEIGHT, NAVIGATION_BAR_HEIGHT - STATUS_BAR_HEIGHT);
        [Btn setTitle:@"关闭" forState:0];
        
        BTN_MSG_FUNC(Btn, hidensss);
        _closeBtn = Btn;
        
    }
    return  _closeBtn;
}


#pragma mark ----------------------- 关闭 ------------------------

-(void)hidensss{
    [_textview resignFirstResponder];
    
    [self removeFromSuperview];
    
}

#pragma mark ----------------------- 提交 ------------------------

-(void)commiteSender:(UIButton *)sender
{
    [_textview resignFirstResponder];
    [self pinglunAction];
    
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [_textview resignFirstResponder];
    
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];//按回车取消第一相应者
    }
    return YES;
}

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    self.textViewPlaceHo.hidden = YES;
    return YES;
}

- (BOOL)textViewShouldEndEditing:(UITextView *)textView
{
    //将要停止编辑(不是第一响应者时)
    if (textView.text.length == 0) {
        self.textViewPlaceHo.hidden = NO;
    }
    return YES;
}

#pragma mark ----------------------- 提交评论内容 ------------------------

-(void)pinglunAction{
    
    [MBProgressHUD showLoadingImageToView:self];
    
    if (_textview.text.length > 200) {
        [MBProgressHUD showTextOnly:self message:@"请输入200字以内的讨论内容" delay:2.0];
        return;
    }
    
    if (_textview.text.length == 0) {
        [MBProgressHUD showTextOnly:self message:@"内容不能为空" delay:2.0];
        return;
    }

    
    SGParameter *parameter  = [[SGParameter alloc] init];
    parameter.body = @{
                      @"task_id":self.course_id,
                       @"content":_textview.text,
                       
                       };
    
    [SGHttpTool POST:[NSString stringWithFormat:@"%@g=Client&m=Comment&a=taskComment",HTTPP] parameter:parameter sucess:^(id responseObject) {
         
        [MBProgressHUD hideHUDForView:self animated:YES];
        
        NSLog(@"%@",responseObject);
        NSDictionary *dic = responseObject;
        
        if ([dic[@"status"] intValue] == 200) {
            
            [self removeFromSuperview];
            self.block();
            [MBProgressHUD showTextOnly:self message:dic[@"msg"] delay:2.0f];

         }else{
            [MBProgressHUD showTextOnly:self message:dic[@"msg"] delay:2.0f];
        }
        
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
        [MBProgressHUD hideHUDForView:self animated:YES];
    }];
    
}

-(UIButton *)commiteBtn{
    if (!_commiteBtn) {
        _commiteBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _commiteBtn.frame = CGRectMake(20 * KscreenWidth,  350 * KscreenHeight, screenWidth - 40 * KscreenWidth, 50 * KscreenHeight);
        _commiteBtn.backgroundColor = ZhuTiColor;
        [_commiteBtn setTitle:@"提交" forState:0];
        BTN_MSG_FUNC(_commiteBtn, commiteSender:);
        
    }
    return _commiteBtn;
    
}


-(UITextView *)textview{
    if (!_textview) {
        UITextView *textView =[[ UITextView alloc]initWithFrame:CGRectMake(20 * KscreenWidth, 110 * KscreenHeight, ScreenWidth-40 * KscreenWidth, 200 * KscreenHeight)];
        
        textView.backgroundColor = [UIColor whiteColor];
        textView.layer.borderColor = [hui2Color CGColor];
        textView.layer.borderWidth = 1;
        textView.font = [UIFont systemFontOfSize:16 * KscreenWidth];
        textView.returnKeyType = UIReturnKeySend;
        textView.delegate = self;
        
        textView.inputAccessoryView = [[UIView alloc] init];
        
        _textview = textView;
        
    }
    return _textview;
}

@end
